<template>
  <el-dialog
    append-to-body
    title="请选择上级字典"
    :visible.sync="visible"
    width="600px">
    <div>
      <el-input
        placeholder="输入关键字进行过滤"
        v-model="filterText">
      </el-input>

      <el-tree
        class="filter-tree"
        :data="dictList"
        :props="defaultProps"
        default-expand-all
        :expand-on-click-node="false"
        :filter-node-method="filterNode"
        @node-click="nodeClick"
        ref="tree">
      </el-tree>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      filterText: '',
      dictList: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      },
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    show({dictList}) {
      this.visible = true
      this.dictList = this.getCanSelectData(dictList)
    },
    hide() {
      this.visible = false
    },
    // 允许编选择的字典
    getCanSelectData(dictList) {
      return dictList.filter(item => {
        return item.title === '退款原因' || item.title === '投诉的责任部门' || item.title === '配送费'
      })
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.title.indexOf(value) !== -1;
    },
    nodeClick(data) {
      if (data.parentId == 0 && data.title != '配送费') {
        this.$message.warning('不可选择一级节点作为上级字典！')
        return
      }
      this.$emit('nodeClick', data)
    }
  },
}
</script>

<style lang="scss" scoped>
.filter-tree {
  max-height: 600px;
  overflow: auto;
}
</style>
